#include <iostream>
#include <cstring>

using namespace std;


class CMyString{
private:
    char *str_;
public:
    CMyString(const char *str = NULL);
    CMyString(const CMyString &other);
    ~CMyString(void);
    CMyString& operator=(const CMyString &other);
};


CMyString::CMyString(const char *str)
{
    if (str == NULL)
    {
        str_ = new char[1];
        str_[0] = '\0';
    }
    else
    {
        str_ = new char[strlen(str) + 1];
        strcpy(str_, str);
    }
}

CMyString::CMyString(const CMyString &other)
{
    str_ = new char[strlen(other.str_) + 1];
    strcpy(str_, other.str_);
}

CMyString::~CMyString(void)
{
    delete [] str_;
}

CMyString& CMyString::operator=(const CMyString &other)
{
    // if (this == &other)
    //     return *this;
    // delete [] str_;
    // str_ = new char[strlen(other.str_) + 1];
    // strcpy(str_, other.str_);
    // cout << "operator= called" << endl;
    // cout << "str_ = " << str_ << endl;
    // return *this;
    if(this != &other)
    {
        CMyString tmp(other);
        char *pTemp = tmp.str_;
        tmp.str_ = str_;
        str_ = pTemp;
    }
    return *this;
}



int main() {
    CMyString str1("hello");
    CMyString str2("world");
    CMyString str3, str4;
    str3 = str4 = str1;
    return 0;
}